package edu.spec.sms.service.impl;

import edu.spec.sms.dao.iface.IStudentDao;
import edu.spec.sms.dao.iface.IStudentQuestionnaireDao;
import edu.spec.sms.dao.iface.IStudentSubjectDao;
import edu.spec.sms.dao.iface.ISubjectDao;
import edu.spec.sms.model.Student;
import edu.spec.sms.model.questionnaire.StudentQuestionnaire;
import edu.spec.sms.model.questionnaire.StudentSubject;
import edu.spec.sms.model.questionnaire.Subject;
import edu.spec.sms.service.iface.IStudentSubjectService;
import edu.spec.sys.model.enumeration.FinishStatus;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.Date;

/**
 * Author       : MadFrog
 * Date         : 2018/7/8 10:51
 * Description  :
 */
@Service
public class StudentSubjectService implements IStudentSubjectService {

    @Inject
    private IStudentDao studentDao;
    @Inject
    private ISubjectDao subjectDao;
    @Inject
    private IStudentSubjectDao studentSubjectDao;
    @Inject
    private IStudentQuestionnaireDao studentQuestionnaireDao;

    @Override
    public void finish(Integer studentId, Integer questionnaireId, String[] array) {
        Student stu = studentDao.load(studentId);
        // 保存小题情况
        for(String l : array){
            String[] group = l.split("-");
            Integer rId = Integer.parseInt(group[0]);
            Integer value = Integer.parseInt(group[1]);
            Subject sub = subjectDao.load(rId);
            StudentSubject sb = new StudentSubject(stu, sub, value);
            studentSubjectDao.add(sb);
        }
        // 更新StudentQuestionnaire
        StudentQuestionnaire sq = studentQuestionnaireDao.load(studentId, questionnaireId);
        sq.setFinishDate(new Date());
        sq.setFinishStatus(FinishStatus.FINISHED);
        studentQuestionnaireDao.update(sq);
    }
}
